package com.lingz.manager.modules.base.controller.manage;

import com.lingz.manager.common.constant.ActivitiConstant;
import com.lingz.manager.common.utils.PageUtil;
import com.lingz.manager.common.utils.ResultUtil;
import com.lingz.manager.common.vo.PageVo;
import com.lingz.manager.common.vo.Result;
import com.lingz.manager.common.vo.SearchVo;
import com.lingz.manager.modules.base.entity.Data;
import com.lingz.manager.modules.base.service.DataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;


/**
 * @author Exrick
 */
@Slf4j
@RestController
@Api(description = "资料内容管理接口")
@RequestMapping("/EnjoyGIS/data")
@Transactional
public class DataController {

    @Autowired
    private DataService dataService;

    @RequestMapping(value = "/getByCondition",method = RequestMethod.GET)
    @ApiOperation(value = "多条件分页获取")
    public Result<Page<Data>> getByCondition(@ModelAttribute Data data,
                                             @ModelAttribute SearchVo searchVo,
                                             @ModelAttribute PageVo pageVo){

        Page<Data> page = dataService.findByCondition(data, searchVo, PageUtil.initPage(pageVo));
        return new ResultUtil<Page<Data>>().setData(page);
    }

    @RequestMapping(value = "/get/{id}",method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<Data> get(@PathVariable String id){

        Data data = dataService.get(id);
        return new ResultUtil<Data>().setData(data);
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ApiOperation(value = "添加资料")
    public Result<Object> addData(@ModelAttribute Data data){
        Data m = dataService.save(data);
        return new ResultUtil<Object>().setSuccessMsg("添加成功");
    }

    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    @ApiOperation(value = "编辑资料")
    public Result<Object> editMessage(@ModelAttribute Data data){

        Data m = dataService.update(data);
        return new ResultUtil<Object>().setSuccessMsg("编辑成功");
    }

    @RequestMapping(value = "/delByIds/{ids}",method = RequestMethod.DELETE)
    @ApiOperation(value = "删除资料")
    public Result<Object> delMessage(@PathVariable String[] ids){

        for(String id:ids){
            if(ActivitiConstant.MESSAGE_PASS_ID.equals(id)||ActivitiConstant.MESSAGE_BACK_ID.equals(id)||ActivitiConstant.MESSAGE_DELEGATE_ID.equals(id)
                    ||ActivitiConstant.MESSAGE_TODO_ID.equals(id)){
                return new ResultUtil<Object>().setErrorMsg("抱歉，无法删除工作流相关系统资料");
            }
            dataService.delete(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("编辑成功");
    }
}
